//
//  NewFeatureController.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/6/28.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit
import FFAutoLayout

private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {
    
    // 设置页面布局
    let layout = UICollectionViewFlowLayout()
    // 重新init方法，是因为外界在实例化该类的时候一般都使用init
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 页面大小
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
    }

    // 定义图片总数
    let imageCount = 4

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.imageIndex = indexPath.item
        
    
        return cell
    }

    // 停止滚动的方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        // 获取当前显示的cell
        let cellPath = collectionView.indexPathsForVisibleItems().last!
        if cellPath.item == imageCount - 1 {
            // 如果是最后一页，就播放动画，显示按钮
            (collectionView.cellForItemAtIndexPath(cellPath) as! NewFeatureCell).showStartButton()
        }
        
    }
    
   
}
/// 自定义cell
class NewFeatureCell: UICollectionViewCell {
    
    // 动画显示按钮
    func showStartButton() {
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        // 显示
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                // 动画完成之后才可以与用户交互
                self.startButton.userInteractionEnabled = true
        }
    }
    // 添加图像索引，根据索引决定当前单元格显示什么图片
    var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            // 默认隐藏开始按钮
            startButton.hidden = true
        }
    }
    
    /// 懒加载图像和自定义按钮
    // 图像
    lazy var iconView: UIImageView = {
        return UIImageView()
        }()
    // 自定义按钮
    lazy var startButton: UIButton = {
        let btn = UIButton()
        btn.frame = CGRectMake(0, 0, 105, 36)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        // 添加点击事件
        btn.addTarget(self, action: "startButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    // 开始按钮被点击
    func startButtonClicked() {
        // 发送通知，其中object为true表示要显示主控制器
        NSNotificationCenter.defaultCenter().postNotificationName(YQSwitchRootVCNotification, object: true)
    }
    
    /// 重写initWithFrame方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 将子控件加入父控件
        addSubview(iconView)
        addSubview(startButton)
        
        // 设置开始按钮的自动布局
        startButton.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: 160))
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        iconView.frame = UIScreen.mainScreen().bounds
    }
}

